#include <rclcpp/rclcpp.hpp>
#include "performance_test_base.h"

namespace trace_api = opentelemetry::trace;
namespace nostd = opentelemetry::nostd;

class QuickPerformanceDemo : public rclcpp::Node {
public:
    QuickPerformanceDemo() : Node("quick_performance_demo") {
        // Initialize OpenTelemetry
        PerformanceTestBase::InitTracer();
        tracer_ = trace_api::Provider::GetTracerProvider()->GetTracer("quick-demo");
        
        RCLCPP_INFO(this->get_logger(), "🚀 Quick OpenTelemetry Performance Demo");
        RCLCPP_INFO(this->get_logger(), "=====================================");
        
        // Run quick tests
        runQuickBasicTest();
        runQuickHighFrequencyTest();
        runQuickAttributeTest();
        
        RCLCPP_INFO(this->get_logger(), "✅ Quick performance demo completed!");
    }

private:
    void runQuickBasicTest() {
        RCLCPP_INFO(this->get_logger(), "📊 测试1: 基础Span创建性能");
        
        PerformanceStats stats;
        stats.reset();
        
        stats.memory_before_kb = PerformanceTestBase::getCurrentMemoryKB();
        auto cpu_before = PerformanceTestBase::getCurrentCPUTime();
        stats.cpu_time_user_before = cpu_before.first;
        stats.cpu_time_system_before = cpu_before.second;
        
        const size_t iterations = 1000; // 减少迭代次数以快速完成
        
        for (size_t i = 0; i < iterations; ++i) {
            auto span_start = std::chrono::high_resolution_clock::now();
            
            auto span = tracer_->StartSpan("quick_basic_test", {
                {"iteration", static_cast<int64_t>(i)}
            });
            
            span->SetAttribute("test.data", "sample_" + std::to_string(i));
            span->AddEvent("quick_event");
            span->SetStatus(trace_api::StatusCode::kOk, "Quick test completed");
            span->End();
            
            auto span_end = std::chrono::high_resolution_clock::now();
            auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(span_end - span_start);
            
            stats.total_time += duration;
            stats.min_time = std::min(stats.min_time, duration);
            stats.max_time = std::max(stats.max_time, duration);
            stats.operation_count++;
        }
        
        stats.memory_after_kb = PerformanceTestBase::getCurrentMemoryKB();
        auto cpu_after = PerformanceTestBase::getCurrentCPUTime();
        stats.cpu_time_user_after = cpu_after.first;
        stats.cpu_time_system_after = cpu_after.second;
        
        stats.calculateAverages();
        PerformanceTestBase::printStats("Quick Basic Span Creation", stats);
    }
    
    void runQuickHighFrequencyTest() {
        RCLCPP_INFO(this->get_logger(), "📊 测试2: 高频Span创建性能");
        
        PerformanceStats stats;
        stats.reset();
        
        stats.memory_before_kb = PerformanceTestBase::getCurrentMemoryKB();
        auto cpu_before = PerformanceTestBase::getCurrentCPUTime();
        stats.cpu_time_user_before = cpu_before.first;
        stats.cpu_time_system_before = cpu_before.second;
        
        const size_t iterations = 5000; // 减少迭代次数
        
        for (size_t i = 0; i < iterations; ++i) {
            auto span_start = std::chrono::high_resolution_clock::now();
            
            auto span = tracer_->StartSpan("quick_high_freq", {
                {"iteration", static_cast<int64_t>(i)}
            });
            
            span->End();
            
            auto span_end = std::chrono::high_resolution_clock::now();
            auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(span_end - span_start);
            
            stats.total_time += duration;
            stats.min_time = std::min(stats.min_time, duration);
            stats.max_time = std::max(stats.max_time, duration);
            stats.operation_count++;
        }
        
        stats.memory_after_kb = PerformanceTestBase::getCurrentMemoryKB();
        auto cpu_after = PerformanceTestBase::getCurrentCPUTime();
        stats.cpu_time_user_after = cpu_after.first;
        stats.cpu_time_system_after = cpu_after.second;
        
        stats.calculateAverages();
        PerformanceTestBase::printStats("Quick High Frequency", stats);
    }
    
    void runQuickAttributeTest() {
        RCLCPP_INFO(this->get_logger(), "📊 测试3: 大属性Span性能");
        
        PerformanceStats stats;
        stats.reset();
        
        stats.memory_before_kb = PerformanceTestBase::getCurrentMemoryKB();
        auto cpu_before = PerformanceTestBase::getCurrentCPUTime();
        stats.cpu_time_user_before = cpu_before.first;
        stats.cpu_time_system_before = cpu_before.second;
        
        const size_t iterations = 100;
        const size_t attributes_per_span = 20; // 减少属性数量
        
        for (size_t i = 0; i < iterations; ++i) {
            auto span_start = std::chrono::high_resolution_clock::now();
            
            auto span = tracer_->StartSpan("quick_large_attr", {
                {"iteration", static_cast<int64_t>(i)}
            });
            
            // Add many attributes
            for (size_t j = 0; j < attributes_per_span; ++j) {
                span->SetAttribute("attr_" + std::to_string(j), 
                    "value_" + std::to_string(i) + "_" + std::to_string(j));
            }
            
            span->End();
            
            auto span_end = std::chrono::high_resolution_clock::now();
            auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(span_end - span_start);
            
            stats.total_time += duration;
            stats.min_time = std::min(stats.min_time, duration);
            stats.max_time = std::max(stats.max_time, duration);
            stats.operation_count++;
        }
        
        stats.memory_after_kb = PerformanceTestBase::getCurrentMemoryKB();
        auto cpu_after = PerformanceTestBase::getCurrentCPUTime();
        stats.cpu_time_user_after = cpu_after.first;
        stats.cpu_time_system_after = cpu_after.second;
        
        stats.calculateAverages();
        PerformanceTestBase::printStats("Quick Large Attribute", stats);
    }

    nostd::shared_ptr<trace_api::Tracer> tracer_;
};

int main(int argc, char* argv[]) {
    rclcpp::init(argc, argv);
    
    auto node = std::make_shared<QuickPerformanceDemo>();
    
    RCLCPP_INFO(node->get_logger(), "🎯 开始快速性能演示...");
    
    rclcpp::spin(node);
    rclcpp::shutdown();
    
    return 0;
}
